package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wallet.dao.AccountInfoDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.model.entity.AccountInfoDo;
import cn.wp.ewallet.wallet.model.entity.AccountInfoDoo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

import static cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant.ACCOUNT_TYPE_AVAILABLE;

/**
 * @ClassName AccountInfoDaoImpl
 * @Author ruixule
 * @Description TODO
 * @Date 2020/1/7 10:55
 */
@Repository
public class AccountInfoDaoImpl implements AccountInfoDao {
    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    /**
     * 通过钱包地址和币种id 查询 账户信息
     *
     * @param walletAddress
     * @param moneyTypeId
     * @return
     */
    @Override
    public AccountInfoDoo selectAccountByWalletAddMoneyTypeId(String walletAddress, String moneyTypeId,String accounttype) {

        SqlString sqlString = new  SqlString();
        sqlString.add("select a.account_id, a.account_name, a.wallet_address, a.money_type_id, a.money_amount,a.accout_type");
        sqlString.add("    , m.money_type_unit, d.contract_address");
        sqlString.add("from account_info a");
        sqlString.add("    left join money_type_info m on a.money_type_id = m.money_type_id");
        sqlString.add("    left join digital_money_info d on m.money_type_id = d.money_type_id");
        sqlString.add("where a.wallet_address = ?");
        sqlString.add("    and a.money_type_id = ?");
        sqlString.add("    and a.accout_type = ?");
        Object[] objects = new Object[]{
                walletAddress,
                moneyTypeId,
                accounttype

        };

        return jdbcSupportTemplate.selectObject(sqlString.build(), objects, AccountInfoDoo.class);
    }

    /**
     * 更新用户的账户金额
     * @param
     * @param
     * @param
     * @return
     */
    @Override
    public int updateAccountInfo(AccountInfoDoo accountInfoDoo) {
        SqlString sqlString = new SqlString();
        sqlString.add("UPDATE  account_info SET MONEY_AMOUNT = ?");
        sqlString.add("WHERE WALLET_ADDRESS = ? AND MONEY_TYPE_ID = ? and ACCOUT_TYPE =?");
        Object[] objects = new Object[]{
                accountInfoDoo.getMoneyAmount(),
                accountInfoDoo.getWalletAddress(),
                accountInfoDoo.getMoneyTypeId(),
                accountInfoDoo.getAccountType(),
        };
        return jdbcSupportTemplate.execute(sqlString.build(),objects);
    }

    /**
     * 保存账户信息
     * @param accountInfoDo
     */
    @Override
    public void insertAccountInfo(AccountInfoDo accountInfoDo) {
        jdbcSupportTemplate.insert(accountInfoDo);
    }

    @Override
    public BigDecimal selectFinaMoneyAmount(String address, String moneyTypeId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select sum(a.money_amount)");
        sqlString.add("from account_info a");
        sqlString.add("where a.wallet_address = ?");
        sqlString.add("    and a.money_type_id = ?");
        sqlString.add("    and (a.accout_type = ?");
        sqlString.add("    or a.accout_type = ?)");

        return jdbcSupportTemplate.selectValue(sqlString.build(),
                new Object[] {
                        address,
                        moneyTypeId,
                        EwalletWalletConstant.ACCOUNT_TYPE_REGULAR,
                        EwalletWalletConstant.ACCOUNT_TYPE_CURRENT
                }, BigDecimal.class);
    }


    /**
     * 通过账户id 查询 账户余额
     * @param accountId
     * @return
     */
    @Override
    public AccountInfoDo selectAccountInfoByAccountId(String accountId) {
        SqlString sqlString=new SqlString();
        sqlString.add("SELECT ACCOUNT_ID, ACCOUNT_NAME, WALLET_ADDRESS, ACCOUT_TYPE, MONEY_TYPE_ID, MONEY_AMOUNT");
        sqlString.add("FROM account_info WHERE ACCOUNT_ID = ?");
       return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{accountId,},AccountInfoDo.class);
    }


    @Override
    public AccountInfoDo selectAccountByWalletAddAccountType(String walletAddress,String accountType) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from ACCOUNT_INFO");
        sqlString.add("where WALLET_ADDRESS = ?");
        sqlString.add("    and ACCOUT_TYPE = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{walletAddress,accountType},AccountInfoDo.class);
    }

    @Override
    public void update(AccountInfoDo accountInfoDo) {
        jdbcSupportTemplate.updateSelective(accountInfoDo);
    }

    @Override
    public void updateAccountInfoById(AccountInfoDo accountInfoDo) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("update account_info");
        sqlString.add("set money_amount = money_amount + ?");
        sqlString.add("where account_id = ?");
        jdbcSupportTemplate.execute(sqlString.build(), new Object[]{accountInfoDo.getMoneyAmount(), accountInfoDo.getAccountId()});
    }

    @Override
    public AccountInfoDo selectAccountInfoByAccountId(String accountId,String moneyTypeId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select * from account_info where ACCOUNT_ID = ? and money_type_id = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{accountId,moneyTypeId},AccountInfoDo.class);
    }

    @Override
    public String selectAddressByAccountId(String accountId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select WALLET_ADDRESS as address");
        sqlString.add("from account_info");
        sqlString.add("where ACCOUNT_ID = ?");
        return jdbcSupportTemplate.selectValue(sqlString.build(),new Object[]{accountId},String.class);
    }

    @Override
    public void increaseMoneyAmount(String accountId, BigDecimal dailyEarn) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("update account_info set money_amount = money_amount + ? where ACCOUNT_ID = ?");
        jdbcSupportTemplate.execute(sqlString.build(),new Object[]{dailyEarn, accountId});
    }

    @Override
    public AccountInfoDoo selectAccountInfoDooByAccountId(String accountId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add(" SELECT a.account_id, a.account_name, a.wallet_address, a.money_type_id, a.money_amount,a.accout_type");
        sqlString.add(", m.money_type_unit, d.contract_address   FROM account_info a");
        sqlString.add("LEFT JOIN money_type_info m ON a.money_type_id = m.money_type_id");
        sqlString.add("LEFT JOIN digital_money_info d ON m.money_type_id = d.money_type_id");
        sqlString.add(" WHERE account_id=?");

        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{accountId},AccountInfoDoo.class);
    }

    @Override
    public int updateAccount(BigDecimal amount, String accountId, String moneyTypeId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("UPDATE account_info SET MONEY_AMOUNT = ? WHERE ACCOUNT_ID = ? AND MONEY_TYPE_ID = ?");
        return jdbcSupportTemplate.execute(sqlString.build(),new Object[]{
                amount,
                accountId,
                moneyTypeId
        });
    }

    @Override
    public void updateAccountInfoDo(AccountInfoDo accountInfoDo) {
        jdbcSupportTemplate.update(accountInfoDo);
    }

}
